from datetime import datetime

from sqlalchemy import Integer, Column, String, Enum, DateTime, ForeignKey, Float, PrimaryKeyConstraint, text
from sqlalchemy.orm import relationship

from src.extension.extension_db import db
class LearningUserProgress(db.Model):
    """学习进度表模型"""
    __tablename__ = 'learning_user_progress'
    __table_args__ = (
        PrimaryKeyConstraint('id', name='learning_user_progress_pkey'),
    )
    id = db.Column(db.Integer, autoincrement=True)
    exercise_id = Column(Integer, nullable=False)
    user_id = db.Column(db.Integer, nullable=False)  # 学生ID
    branch_id = db.Column(String, nullable=False)  # 任务ID
    start_time = db.Column(db.DateTime, nullable=False)  # 学习开始时间
    end_time = db.Column(db.DateTime, nullable=True)  # 学习结束时间
    duration = db.Column(db.Float, nullable=True)  # 学习时长（单位：小时）
    updated_at = Column(DateTime, nullable=False, server_default=text('CURRENT_TIMESTAMP(0)'),
                        server_onupdate=text('CURRENT_TIMESTAMP(0)'))
    created_ar = Column(DateTime, nullable=False, server_default=text('CURRENT_TIMESTAMP(0)'),
                        server_onupdate=text('CURRENT_TIMESTAMP(0)'))
    @property
    def completion_percentage(self) -> float:
        """计算学习进度的完成百分比"""
        return (self.completed_chapters / self.total_chapters) * 100 if self.total_chapters else 0.0

